<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/5
 * Time: 16:03
 */

namespace App\Http\Admin\Administration\Models;

use Framework\BaseClass\Http\Model;
use App\Eloquent\Oa\ConferenceRoom as EloquentConferenceRoom;


class ConferenceRoom extends Model
{
    /**
     * 获取会议室列表
     * @Author sxy
     * @param $page
     * @param $pageSize
     * @param $searchField
     * @return mixed
     */
    public function getConferenceRoomList($page, $pageSize, $searchField)
    {
        $list = EloquentConferenceRoom::where($searchField);


        $count = $list->count();
        $list = $list->forPage($page, $pageSize)->get();
        foreach ($list as $v){
            if($v['end_time']>time()){
                $v['status']="已预约";
            }else{
                $v['status']="未预约";
            }
        }

        $data['list'] = $list;
        $data['pageCurrent'] = $page;
        $data['totalRow'] = $count;
        return $data;
    }

    /**
     * 添加会议室
     * @Author sxy
     * @param $request
     * @return bool
     */
    public function addConferenceRoom($request){
        $conferenceRoom = new EloquentConferenceRoom();
        $conferenceRoom->name = $request['name'];
        $conferenceRoom->content = htmlspecialchars_decode($request['content']);
        $conferenceRoom->main_image_url = $request['main_image_url'];
        $conferenceRoom->area = $request['area'];
        $conferenceRoom->position = $request['position'];
        $conferenceRoom->created_at = time();
        $conferenceRoom->oa_company_id = $request['oa_company_id'];
        $result = $conferenceRoom->save();
        if ($result) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 编辑会议室
     * @Author sxy
     * @param $request
     * @return bool|void
     */
    public function editConferenceRoom($request)
    {
        $lawPost = EloquentConferenceRoom::where('id', $request['id'])->first();
        if (!$lawPost) {
            return xThrow(ERR_CONFERENCE_ROOM_NOT_EXIST);
        }
        $input = [
            'name' => $request['name'],
            'content' => htmlspecialchars_decode($request['content']),
            'area' => $request['area'],
            'position' => $request['position'],
            'updated_at' => time(),
        ];
        if (isset($request['main_image_url'])) {
            $input['main_image_url'] = $request['main_image_url'];
        }
        $result = $lawPost->update($input);


        if ($result) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取会议室编辑记录
     * @Author sxy
     * @param $id
     * @return \Illuminate\Database\Eloquent\Model|null|static
     */
    public function getConferenceRoomInfo($id){
        $result = EloquentConferenceRoom::where('id', '=', $id)->first();
        if (is_null($result)) xThrow(ERR_CONFERENCE_ROOM_NOT_EXIST);
        return $result;
    }




}